package handler

import (
	"common"
	"common/biz"
	"common/logs"
	"context"
	"core/repo"
	"core/service"
	"encoding/json"
	"framework/remote"
	"hall/logic"
	"hall/models/request"
	"strconv"
)

type ProductionHandler struct {
	productionService *service.ProductionService
	warehouseService  *service.WarehouseService
	pm                *logic.ProductionManager
	base              *BaseHandler
}

func (h *ProductionHandler) ProductionList(session *remote.Session, msg []byte) any {
	logs.Info("[productionHandler] ProductionList msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[productionHandler] ProductionList err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	ctx := context.TODO()
	mineral := h.warehouseService.WarehouseMaterial(ctx, uint(uid), 11)
	data := h.productionService.ProductionList(ctx, uint(uid))
	return common.S(map[string]any{
		"mineral":    mineral.Num,
		"production": data,
	})
}

func (h *ProductionHandler) ProductionOpen(session *remote.Session, msg []byte) any {
	logs.Info("[productionHandler] ProductionOpen msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[productionHandler] ProductionOpen err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.CommonID
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[productionHandler]用户%d ProductionOpen err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	ctx := context.TODO()
	err := h.productionService.ProductionOpen(ctx, uint(uid), req.ID)
	if err != nil {
		return common.F(err)
	}
	return common.S(nil)
}

func (h *ProductionHandler) ProductionInstallOfDisassembly(session *remote.Session, msg []byte) any {
	logs.Info("[productionHandler] ProductionInstallOfDisassembly msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[productionHandler] ProductionInstallOfDisassembly err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.ProductionInstallOfDisassembly
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[productionHandler]用户%d ProductionInstallOfDisassembly err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	ctx := context.TODO()
	err := h.productionService.ProductionInstallOfDisassembly(ctx, uint(uid), &req)
	if err != nil {
		return common.F(err)
	}
	return common.S(nil)
}

func (h *ProductionHandler) ProductionStart(session *remote.Session, msg []byte) any {
	logs.Info("[productionHandler] ProductionStart msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[productionHandler] ProductionStart err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.CommonID
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[productionHandler]用户%d ProductionStart err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	ctx := context.TODO()
	data, err := h.pm.ProductionStart(ctx, uint(uid), req.ID, session)
	if err != nil {
		return common.F(err)
	}
	return common.S(data)
}

func (h *ProductionHandler) ProductionReceive(session *remote.Session, msg []byte) any {
	logs.Info("[productionHandler] ProductionStart msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[productionHandler] ProductionStart err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.CommonID
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[productionHandler]用户%d ProductionStart err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	ctx := context.TODO()
	err := h.productionService.ProductionReceive(ctx, uint(uid), req.ID)
	if err != nil {
		return common.F(err)
	}
	return common.S(nil)
}

func NewProductionHandler(r *repo.Manager, pm *logic.ProductionManager) *ProductionHandler {
	return &ProductionHandler{
		productionService: service.NewProductionService(r),
		warehouseService:  service.NewWarehouseService(r),
		base:              NewBaseHandler(r),
		pm:                pm,
	}
}
